#include "Bishop.h"
#include "../Game/Engine.h"
#include "../Board/Chessboard.h"
#include "../Board/Square.h"

#include "pthread.h"

namespace Core
{
	Bishop::Bishop(Player * player, Square * square)
		:Piece(player, square)
	{
	}


	Bishop::~Bishop(void)
	{
	}

	std::string Bishop::name() const
	{ 
		return "Bishop"; 
	}

	ChType Bishop::type() const
	{ 
		return ChType::ChBishop; 
	}

	//Move * Bishop::canMove(Square * dst)
	//{
	//	Move * mv = Move::createMove(this, dst);

	//	return mv;
	//}

	void Bishop::updateValidMoves()
	{
		int r = this->square()->row();
		int c = this->square()->column();
		
		considerForValidMoves(r+1, c+1);
		considerForValidMoves(r+1, c-1);
		considerForValidMoves(r-1, c+1);
		considerForValidMoves(r-1, c-1);
	}

	void Bishop::considerForValidMoves(int r2, int c2)
	{
		const Chessboard * brd = Engine::instance()->chessboard();
		try
		{
			Piece * p = brd->findPieceDiagonal(square()->row(), square()->column(), r2, c2, true, &_validMoves); // might throw exception
		}
		catch(...) { }
	}

	/*
	void Bishop::addRangeOfValidMoves(int r1, int c1, int r2, int c2, Piece* p)
	{
		int dr = Chessboard::sign(r2 - r1);
		int dc = Chessboard::sign(c2 - c1);

		while(1)
		{
			r1 += dr;
			c1 += dc;

			if(p != NULL)
			{
				int r3 = p->square()->row();
				int c3 = p->square()->column();

				if(p->color() == this->color() || p->type() == ChKing) // could not eat the piece
				{
					if(r1 == r3 || c1 == c3)
						break;
				}
				else if(r1 == r3 + dr || c1 == c3 + dc)
					break;
			}
			try
			{
				Move * mv = Move::createMove(this, Engine::instance()->chessboard()->at(r1, c1));
				_validMoves[mv->to()->key()] = mv;
			}
			catch(...) 
			{
				// if out of range: break the loop!
				break; 
			}

		}
	}
	*/


}
